<?php
/**
 *
 * @authors xiadx (xiadx@zpdchina.com)
 * @date    2017-03-06 14:54:58
 * @version $Id$
 */

class DataHulijiluTemplateModel extends BaseModel {

	/**
     * 返回给终端的可执行sql语句的字段名称数组
     * @var [type]
     */
    public $_field_arr = ['id','xiangmu_name','hulicol_name','xiangmu_leixing', 'xiangmu_value','paixu','biaodan_name','biaodan_leibie','keshi','default_value','accept_char', 'input_type', 'xiangmu_value_save', 'reeditor_enable','show_zhuangtai', 'keyboardname'];

    public function ls($user_department='', $temp_id_arr = [], $biaodan_name ='')
    {
    	$field_str = implode(",", $this->_field_arr);
    	$query = DB::select($field_str)
    				->from('data_hulijilu_template')
    				->where('yiyuan_id', '=', $this->yiyuan_id)
    				->and_where_open()
    					->where('keshi', '=', '通用')
    					->or_where('keshi', 'LIKE', "%$user_department%")
    				->and_where_close();

		if (!empty($temp_id_arr)) {
			$query->and_where('id', 'IN', $temp_id_arr);
		}
		//add by Hex @20170310 for 新增表单条件筛选
		if (!empty($biaodan_name)) {
			$query->and_where('biaodan_name', '=', $biaodan_name);
		}
		$data = $query->execute()
			->as_array();
    	return $data;
    }

    /**
     * 根据科室获取表单名称
     * @param  string $user_department [description]
     * @return [type]                  [description]
     */
    public function getBiaodanNameByKeshi($user_department='')
    {
    	$query = DB::select('biaodan_name')
    				->distinct(TRUE)
    				->from('data_hulijilu_template')
    				->where('yiyuan_id', '=', $this->yiyuan_id)
    				->and_where_open()
    					->where('keshi', '=', '通用')
    					->or_where('keshi', 'LIKE', "%$user_department%")
    				->and_where_close()
    				->execute()
    				->as_array();
		return $query;
    }

    /**
     * 根据id获取表单名称
     * @param  array  $huli_temp_id_arrs [description]
     * @return [type]                    [description]
     */
    public function getXiangmuNameBySubId($huli_temp_id_arrs = [],$field = '')
    {
    	if (is_array($huli_temp_id_arrs) && empty($huli_temp_id_arrs)) {
    		return [];
    	}
    	$query = DB::select('id,biaodan_name,xiangmu_name')
    				->from('data_hulijilu_template')
    				->where('yiyuan_id', '=', $this->yiyuan_id)
    				->and_where('id', 'IN', $huli_temp_id_arrs)
    				->execute()
    				->as_array();
		$data = [];
    	foreach ($query as $value) {
    		foreach ($huli_temp_id_arrs as $id => $huli_temp_id) {
    			if ($value['id'] == $huli_temp_id) {
    				$data[$id] = isset($value[$field]) ? $value[$field] : '';
    			}
    		}
    	}
    	return $data;
    }

    /**
     * 多编辑升级版的主表、子表的数据组号
     * @param  string $user_department [description]
     * @param  array  $temp_id_arr     [以主表的id为键，子表的id为值的一维数组]
     * @return [type]                  [description]
     */
    public function lsSubItem($user_department='', $temp_id_arr = [], $per_number = 100)
    {
    	$ids = array_column($temp_id_arr,'template_id');
    	if(empty($ids) || empty($temp_id_arr)) return [];
    	$field_str = implode(",", $this->_field_arr);
    	$query = DB::select($field_str)
    				->from('data_hulijilu_template')
    				->where('yiyuan_id', '=', $this->yiyuan_id)
    				->and_where_open()
    					->where('keshi', '=', '通用')
    					->or_where('keshi', 'LIKE', "%$user_department%")
    				->and_where_close();
		if (!empty($ids)) {
			$query = $query->and_where('id', 'IN', $ids);
		}
		$data = $query->execute()->as_array();
		if($data){
			foreach($data as $v){
				$da[$v['id']] = $v;
			}
		}
		
		$sub_ids = array_column($temp_id_arr,'sub_template_id');
		/*合并子表的数据到主表中*/
        if ($sub_ids) {
        	/*取子表数据*/
        	$sub_item = [];
        	$sub_item = DB::select('id,input_type,sort,name,val_name,xiangm_value,max,min,danwei')
        	->from('data_hulijilu_template_sub')
        	->where('id', 'IN', $sub_ids)
        	->and_where('yiyuan_id', '=', $this->yiyuan_id)
        	->execute()
        	->as_array();
        	if($sub_item){
        		foreach($sub_item as $v){
        			$sub[$v['id']] = $v;
        		}
        	}
        	unset($sub_item);
        }
        $data = [];
        $k = 0;
        $j = 0;
        $num = 1;
		foreach ($temp_id_arr as $t) {
			if($da[$t['template_id']] && $sub[$t['sub_template_id']]){
				$sub_item = $sub[$t['sub_template_id']];
				$v = $da[$t['template_id']];
				//0下拉框、1输入框、2文本框、3时间框、4多选框
				$edit_number = $sub_item['input_type']=='oneselect'?0:1;
				if($k==0){
					$data[$j].= "REPLACE INTO data_hulijilu_multiedit_template(id,paixu,biaodan_type,leixing,xiangmu_name,val_name,edit_number,xiangmu_value,max,min,danwei,input_type,accept_value,keyboardname) SELECT '".$num."','".$sub_item["sort"]."','".$v["biaodan_name"]."','".$v["xiangmu_name"]."','".$sub_item["name"]."','".$sub_item["val_name"]."','".$edit_number."','".$sub_item["xiangm_value"]."','".$sub_item["max"]."','".$sub_item["min"]."','".$sub_item["danwei"]."','".$v["input_type"]."','".$v["accept_char"]."','".$sub_item["keyboardname"]."'";
				} else {
					$data[$j].= " union all SELECT '".$num."','".$sub_item["sort"]."','".$v["biaodan_name"]."','".$v["xiangmu_name"]."','".$sub_item["name"]."','".$sub_item["val_name"]."','".$edit_number."','".$sub_item["xiangm_value"]."','".$sub_item["max"]."','".$sub_item["min"]."','".$sub_item["danwei"]."','".$v["input_type"]."','".$v["accept_char"]."','".$sub_item["keyboardname"]."'";
				}
				$k++;$num++;
				if($k == $per_number){
					$k=0;$j++;
				}
			}
		}
    	return $data;
    }
}
